﻿namespace OpenAction.MydraVideoOpenActionContextProvider.Test
{
    using System;
    using System.IO;
    using System.Linq;

    using MEF_Test.Bricks.Subtitles;
    using MEF_Test.Contracts;

    using Microsoft.Practices.Prism.Regions;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using Moq;

    using Subtitles.Formats.Srt;
    using Subtitles.MydraSubtitlesSelection;

    [TestClass]
    public class SubtitlesSelectionPageViewModelTests
    {
        /// <summary>
        /// Tests the subtitle data are correctly built.
        /// </summary>
        [TestMethod]
        public void TestSubtitleDataAreCorrectlyBuilt()
        {
            // prepairing test

            // I: Prepair a browser node view model which is a dependency to be injected. ( Note : is it really a necessary dependency in the ViewModel for the subtitles selection page ??? )
            var mock = new Mock<IBrowserNodeViewModel>(MockBehavior.Strict);
            mock.Setup(o => o.MediaHandler).Returns(new Mock<IVideoMediaHandler>(MockBehavior.Strict).Object);
            IBrowserNodeViewModel node = mock.Object;

            // II : Instanciate the SubtitlesSelectionPageViewModel while injecting the needed dependencies.
            ISubtitlesProvidersManager subtitlesProvidersManager = new Mock<ISubtitlesProvidersManager>(MockBehavior.Strict).Object;
            SubtitlesSelectionPageViewModel subtitlesSelectionPageViewModel = new SubtitlesSelectionPageViewModel(subtitlesProvidersManager, node, new Mock<IRegion>().Object,new Mock<IVideoRendererViewFactory>().Object);


            // III : Prepair the selected SubtitleViewModel to parse.
            var selectedSubtitleMock = new Mock<ISubtitleViewModel>(MockBehavior.Strict);
            
            // setup the return value for the subtitles text.
            Stream srtContent = new MemoryStream(Resource1.sub);            
            selectedSubtitleMock.Setup(o => o.GetSubtitleFile()).Returns(srtContent);
            selectedSubtitleMock.Setup(o => o.SubtitleFormatHandler).Returns(new SrtSubtitleFormatHandler());

            ISubtitleViewModel selectedSubtitle = selectedSubtitleMock.Object;

            // Actual test : Test the subtitlesSelectionPageViewModel.BuildSubtitleData method
            var subtitleHandler = subtitlesSelectionPageViewModel.BuildSubtitleData(selectedSubtitle);

            Assert.AreEqual(536, subtitleHandler.SubtitleEntries.Count(), "The provided subtitle has not the same amount of dialog lines as the produced subtitle data entries.");
            Assert.AreEqual(TimeSpan.Parse("0:0:0.5"),subtitleHandler.SubtitleEntries.First().Start,"The start time is not valid."); 
        }
    }
}
